package tt.dz.biz.service.smssend;

import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import tt.dz.biz.service.smsConfigure.SmsConfigureService;
import tt.dz.common.BusinessException;
import tt.dz.vo.SmsConfigureVO;

@Service("YIMEISMSMessage")
@Scope("prototype")
public class YIMEISMSMessage
  implements SendMessageConfigAwareI
{
  @Autowired
  @Qualifier("smsConfigureService")
  private SmsConfigureService smsConfigureService;
  private String CDKEY_KEY = "cdkey";
  private String PASSWORD_KEY = "password";
  private SmsConfigureVO smsConfig;
  private boolean isSend;
  
  public String sendMessage(String phone, String message)
    throws Exception
  {
    if (!this.isSend) {
      return "";
    }
    Map<String, String> map = new HashMap();
    map = getConfigureMap();
    map.put("apikey", map.get(this.CDKEY_KEY));
//    map.put("password", map.get(this.PASSWORD_KEY));
    map.put("mobile", phone);
    map.put("content", message);
    String xml = SendSMSHelpHandler.post(this.smsConfig.getUrl(), map, "UTF-8");
    return xml;
  }
  
  public void isAbleSend(boolean isSend)
  {
    this.isSend = isSend;
  }
  
  private Map<String, String> getConfigureMap()
  {
    Map<String, String> map = this.smsConfigureService.convertParamMap(this.smsConfig);
    if ((map == null) || (map.isEmpty())) {
      throw new BusinessException("未找到对应的短信参数配置列表");
    }
    return map;
  }
  
  public void setSmsConfig(SmsConfigureVO smsConfig)
  {
    this.smsConfig = smsConfig;
  }
}
